package com.bsbm.common.bean;

import org.richfaces.component.UITree;
import org.richfaces.event.TreeSelectionChangeEvent;
import org.richfaces.event.TreeToggleEvent;
import org.richfaces.model.TreeNodeImpl;

import javax.swing.tree.TreeNode;
import java.io.File;
import java.io.FileFilter;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by SABIR IQBAL on 7/9/2014.
 */
public class FileSystemBean implements Serializable {
    private List<FileSystemNode> driveNodes;
    private FileSystemNode selectedNode;

    public synchronized List<FileSystemNode> getRootDrives() {
        if (this.driveNodes == null) {
            driveNodes = new ArrayList<FileSystemNode>();
            for (File file : File.listRoots()) {
                driveNodes.add(new FileSystemNode(file, true));
            }
        }
        return driveNodes;
    }

    public void processTreeSelectionChange(TreeSelectionChangeEvent event) {
        List<Object> selection = new ArrayList<Object>(event.getNewSelection());
        Object currentSelectionKey = selection.get(0);
        UITree tree = (UITree) event.getSource();
        tree.setRowKey(currentSelectionKey);
        this.selectedNode = (FileSystemNode)tree.getRowData();

    }
    public void processTreeToggle(TreeToggleEvent event){
         System.out.print("I am happy.."+event);
        //List<Object> selection = new ArrayList<Object>(event.getNewSelection());
        //Object currentSelectionKey = selection.get(0);
        UITree tree = (UITree) event.getSource();

        Object storedKey = tree.getRowKey();
        //tree.setRowKey(currentSelectionKey);
        //TreeNode currentSelection = (TreeNode) tree.getRowData();
        //tree.setRowKey(storedKey);
    }
    public static class FileSystemNode extends TreeNodeImpl {

        private final File file;
        private final boolean isDrive;
        private List<FileSystemNode> directories;

        public FileSystemNode(File file, boolean isDrive) {
            this.file = file;
            this.isDrive = isDrive;
        }
        public String getType() {
            if (this.isDrive) {
                return "drive";
            } else {
                return "directory";
            }
        }
        public String getName() {
            if (this.isDrive) {
                return this.file.getAbsolutePath();
            } else {
                return this.file.getName();
            }
        }
        public File getFile() {
            return this.file;
        }
        public synchronized List<FileSystemNode> getDirectories() {
            if (this.directories == null) {
                this.directories = new ArrayList<FileSystemNode>();
                for (File child : this.file.listFiles(new DirectoryFilter())) {
                    this.directories.add(new FileSystemNode(child, false));
                }
            }
            return this.directories;
        }
        @Override
        public boolean isLeaf() {
            File[] children = this.file.listFiles(new DirectoryFilter());
            if (children == null || children.length == 0) {
                return true;
            }
            return false;
        }
    }
    public FileSystemNode getSelectedNode() {
        return this.selectedNode;
    }
    /**
     *
     */
    public static class DirectoryFilter implements FileFilter {
        @Override
        public boolean accept(File file) {
            return file.isDirectory();
        }
    }

}
